package net.news.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import net.news.entity.News;

import com.liesp.base.BaseDAO;
import com.liesp.util.PropertyFilter;

/**
 * 项目名称：cmunews3084<br>
 * 类名称：NewsDao <br>
 * 类描述： TODO(请输入类的描述) <br>
 * 创建人：charles 创建时间：2011-5-8 下午11:30:55 <br>
 * 修改人： 修改时间： <br>
 * 修改备注：
 * 
 * @version V1.0
 */
@Repository
public class NewsDao extends BaseDAO<News, Integer> {

	/**
	 * 保存新闻方法
	 * 
	 * @param news
	 *            新闻实体类
	 */
	public void savaNews(News news) {
		this.save(news);
	}

	/**
	 * 查询所有新闻方法
	 * 
	 * @return
	 */
	public List<News> findNewsList() {
		String hql = "from News as n left join fetch n.newsType left join fetch n.newsPeople order by n.id desc";
		return this.findByHql(hql);
	}

	/**根据id查询新闻
	 * @param news 新闻实体
	 * @return
	 */
	public List<News> findNewsListById(News news) {
		String hql = "from News as  n left join fetch n.newsType left join fetch n.newsPeople where n.id=?";
		return this.findByHql(hql,new Object[]{news.getId()});
	}
	
	/**根据条件模糊查询方法
	 * @param filters
	 * @return
	 */
	public List<News> findNewsQueryOr(List<PropertyFilter> filters,String[] claName){
		String[] orderName=new String[]{"newsTime"};
		String[] orderEsc=new String[]{"desc"};
		return this.find(filters,claName,orderName,orderEsc);
	}
	
	/**根据新闻类类型查询新闻
	 * @param newstypeid
	 * @return
	 */
	public List<News> findNewsByNewsType(int newstypeid){
		String hql="from News as  n left join fetch n.newsPeople left join fetch n.newsType as nt where nt.id=? order by n.newsTime desc";
		return this.findByHql(hql,new Object[]{newstypeid});
	}
	
	/**查询点击率最高的一条新闻
	 * @return
	 */
	public List<News> findNewsHot(){
		String hql="from News as n left join fetch n.newsPeople left join fetch n.newsType as nt order by n.clickRate desc";
		return this.findByHql(hql);
	}
	
	/**删除新闻方法
	 * @param news
	 */
	public void deleteNews(News news){
		this.delete(news);
	}
	
	/**跟新新闻方法
	 * @param news
	 */
	public void updateNews(News news){
		this.merge(news);
	}
}
